//
//  OrderCell.swift
//  订单列表 cell
//
//  Created by 千策万划-mini on 2025/5/8.
//

import Foundation

class OrderCell: UITableViewCell {
    
    // MARK: - lazy
    lazy var imageContentBg: UIImageView = {
        let result = UIImageView()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(345), W(162))
        result.image = UIImage(named: "icon_order_cell_content_bg")
        return result
    }()
    lazy var imageLogo: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(40), W(40))
        result.contentMode = .scaleAspectFill;
        result.clipsToBounds = true;
        return result
    }()
    lazy var labelName: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var btnRightTop: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(95), W(44))
        result.setBackgroundImage(UIImage(named: "icon_login_code_btn"), for: .normal)
        return result
    }()
    lazy var labelBtnTitle: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var labelAmountTitle: UILabel = {
        let result = UILabel()
        result.textColor = UIColor(hex: "#5F2F0D")
        result.font = UIFont.customMediumFont(size: 14)
        return result
    }()
    lazy var labelAmount: UILabel = {
        let result = UILabel()
        result.textColor = UIColor(hex: "#2D0084")
        result.font = UIFont.customExtraBoldFont(size: 24)
        return result
    }()
    lazy var labelStateTitle: UILabel = {
        let result = UILabel()
        result.textColor = UIColor(hex: "#5F2F0D")
        result.font = UIFont.customMediumFont(size: 14)
        return result
    }()
    lazy var imageStateBg: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(90), W(36))
        result.image = UIImage(named: "icon_order_state_bg")
        return result
    }()
    lazy var labelState: UILabel = {
        let result = UILabel()
        result.font = UIFont.customBoldFont(size: 14)
        return result
    }()
    lazy var labelDateTitle: UILabel = {
        let result = UILabel()
        result.textColor = UIColor(hex: "#5F2F0D")
        result.font = UIFont.customMediumFont(size: 14)
        return result
    }()
    lazy var labelDate: UILabel = {
        let result = UILabel()
        result.textColor = UIColor(hex: "#2D0084")
        result.font = UIFont.customSemiBoldFont(size: 16)
        return result
    }()
    lazy var imageTipBg: UIImageView = {
        let result = UIImageView()
        result.width = W(345)
        result.image = UIImage(named: "icon_order_tip_bg")
        return result
    }()
    lazy var imageTipBell: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(14), W(16))
        result.image = UIImage(named: "icon_order_bell")
        return result
    }()
    lazy var labelTip: UILabel = {
        let result = UILabel()
        result.numberOfLines = 0
        result.lineSpace = 0
        result.textColor = COLOR_RED()
        result.font = UIFont.customMediumFont(size: 12)
        return result
    }()
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = COLOR_BACKGROUND
        self.backgroundColor = COLOR_BACKGROUND
        self.selectionStyle = .none
        setUpView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpView() {
        contentView.addSubview(imageContentBg)
        imageContentBg.addSubview(imageLogo)
        imageContentBg.addSubview(labelName)
        imageContentBg.addSubview(btnRightTop)
        btnRightTop.addSubview(labelBtnTitle)
        
        imageContentBg.addSubview(labelAmountTitle)
        imageContentBg.addSubview(labelAmount)
        
        imageContentBg.addSubview(labelDateTitle)
        imageContentBg.addSubview(labelDate)
        
        imageContentBg.addSubview(labelStateTitle)
        imageContentBg.addSubview(imageStateBg)
        imageStateBg.addSubview(labelState)
        
        contentView.addSubview(imageTipBg)
        imageTipBg.addSubview(imageTipBell)
        imageTipBg.addSubview(labelTip)
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        switch sender.tag {
        case 1:
            
            break
        case 2:
            
            
            break
        default:
            break
        }
    }
    
    
    
    
    // MARK: - 重置界面
    @objc func resetCellWithModel(_ result: Any){
        guard let model = result as? ModelOrderTeasingInfo else { return }
        
        imageContentBg.centerXTop = XY(SCREEN_WIDTH/2, W(15))
        imageLogo.leftTop = XY(W(15), W(15))
        imageLogo.setImage(urlStr: model.prison.lucky)
        
        labelName.resetView(
            title: UnPackStr(model.prison.sooner),
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 20),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        labelName.leftCenterY = XY(imageLogo.right+5, imageLogo.centerY)
        
        btnRightTop.isHidden = !isStr(model.prison.set)
        btnRightTop.rightCenterY = XY(imageContentBg.width-imageLogo.left, imageLogo.centerY)
        labelBtnTitle.resetView(
            title: UnPackStr(model.prison.set),
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#004600"),
            outlineColor: UIColor(hex: "#1B4506")
        )
        labelBtnTitle.centerXCenterY = XY(btnRightTop.width/2, btnRightTop.height/2)
        
        labelAmountTitle.fitTitle(UnPackStr(model.prison.weeny), variable: 0)
        labelAmountTitle.leftTop = XY(imageLogo.left, imageLogo.bottom+W(28))
        labelAmount.fitTitle(UnPackStr(model.prison.teeny), variable: 0)
        labelAmount.leftTop = XY(labelAmountTitle.left, labelAmountTitle.bottom+W(8))
        
        
        labelStateTitle.fitTitle("State", variable: 0)
        labelStateTitle.centerXCenterY = XY(imageContentBg.width/2, labelAmountTitle.centerY)
        imageStateBg.centerXCenterY = XY(labelStateTitle.centerX, labelAmount.centerY)
        labelState.textColor = COLOR_RED()
        if model.prison.sprung == 1 {
            labelState.textColor = COLOR_RED()
        }else if model.prison.sprung == 2 {
            labelState.textColor = UIColor(hex: "#B15502")
        }else if model.prison.sprung == 3 {
            labelState.textColor = UIColor(hex: "#077200")
        }else if model.prison.sprung == 4 {
            labelState.textColor = UIColor(hex: "#2D0084")
        }else if model.prison.sprung == 5 {
            labelState.textColor = UIColor(hex: "#B6B3BB")
        }
        labelState.fitTitle(UnPackStr(model.prison.hop), variable: 0)
        labelState.centerXCenterY = XY(imageStateBg.width/2, imageStateBg.height/2)
        
        labelDateTitle.fitTitle(UnPackStr(model.prison.backed), variable: 0)
        labelDateTitle.rightCenterY = XY(btnRightTop.right, labelAmountTitle.centerY)
        labelDate.fitTitle(UnPackStr(model.prison.stuck), variable: 0)
        labelDate.rightCenterY = XY(labelDateTitle.right, labelAmount.centerY)
        
        imageTipBg.isHidden = !isStr(model.prison.traps)
        imageTipBell.left = W(15)
        labelTip.fitTitle(UnPackStr(model.prison.traps), variable: imageTipBg.width-imageTipBell.right-W(5))
        labelTip.leftTop = XY(imageTipBell.right+W(5), W(11))
        imageTipBg.height = max(W(34), labelTip.bottom+labelTip.top)
        imageTipBg.centerXTop = XY(SCREEN_WIDTH/2, imageContentBg.bottom+W(6))
        imageTipBell.centerY = labelTip.centerY
        
        height = imageTipBg.isHidden == false ? (imageTipBg.bottom) : imageContentBg.bottom
    }
}
